//
//  IFlySystemInfo.m
//  AIKITDemo
//
//  Created by xyzhang on 2023/3/1.
//

#import "IFlySystemInfo.h"
#import <mach/mach.h>

#import <UIKit/UIKit.h>

#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <CoreTelephony/CTCarrier.h>

@implementation IFlySystemInfo

NSString *replaceSeparator (NSString *str) {
    return  [str stringByReplacingOccurrencesOfString:@"," withString:@"."];
}

+ (NSString *)replaceSeparate:(NSString *)inputStr {
    return  [inputStr stringByReplacingOccurrencesOfString:@"," withString:@"."];
}

#pragma mark - device info

// e.g iOS
+ (NSString *)getSystemName {
    return [[UIDevice currentDevice] systemName];
}

+ (NSString *)getSystemMainVersion {
    NSString *systemVersion = [[UIDevice currentDevice] systemVersion];
    NSRange range = [systemVersion rangeOfString:@"."];
    if([systemVersion length]>0 && range.length > 0){
        NSString *osMainVersion = [systemVersion substringToIndex:range.location];
        return osMainVersion;
    }else{
        return @"";
    }
}

+ (NSString *)getSystemVersion {
    return  [[UIDevice currentDevice] systemVersion];
}

/// 获取系统屏幕分辨率
+ (NSString *)getSystemScreenResolution {
    CGSize size = [[[UIScreen mainScreen] currentMode] size];
    NSString *systemScreenResolution = [NSString stringWithFormat:@"%0.f*%0.f", size.width, size.height];
    return systemScreenResolution;
}

+ (NSString *)getManufact {
    return @"Apple";
}

+ (NSString *)getCarrierInfo {
    CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc]init];
    CTCarrier *carrier = [info subscriberCellularProvider];
    NSString *carrierInfo = [NSString stringWithFormat:@"carrierName=%@,mobileCountryCode=%@,mobileNetworkCode=%@,isoCountryCode=%@,allowsVOIP=%@", carrier.carrierName, carrier.mobileCountryCode, carrier.mobileNetworkCode, carrier.isoCountryCode, carrier.allowsVOIP?@"true":@"false"];
//    NSLog(@"carrierInfo=%@",carrierInfo);
    return carrierInfo;
}

+ (NSString *)getCarrierName {
    CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc]init];
    CTCarrier *carrier = [info subscriberCellularProvider];
    NSString *carrierName = carrier.carrierName;
    if (carrierName.length>0) {
        return carrierName;
    } else {
        return @"";
    }
}



#pragma mark - app info

#pragma mark - other info

+ (NSString *)getPhysicalMemory {
    unsigned long long physicalMemory = [NSProcessInfo processInfo].physicalMemory;
    NSLog(@"mem: %lld G", physicalMemory/(1024*1024*1024));
    return [NSString stringWithFormat:@"%lld",physicalMemory];
}

+ (int)getMemoryUsage {
    int64_t memoryUsageInByte;
    task_vm_info_data_t vmInfo;
    mach_msg_type_number_t count = TASK_VM_INFO_COUNT;
    kern_return_t kernelReturn = task_info(mach_task_self(), TASK_VM_INFO, (task_info_t) &vmInfo, &count);
    if(kernelReturn == KERN_SUCCESS) {
        memoryUsageInByte = (int64_t) vmInfo.phys_footprint;
//        NSLog(@"Memory in use (in bytes): %lld", memoryUsageInByte);
    } else {
//        NSLog(@"Error with task_info(): %s", mach_error_string(kernelReturn));
    }
    // MB
    int memoryUsageInMillonByte = (int)memoryUsageInByte / (1024*1024);
    return memoryUsageInMillonByte;
}

@end
